package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RankClassAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.db.SongListEntity;
import com.lanou.sfu.songforu.entity.AlbumDetailEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.ListViewForScrollView;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/11/9.
 */
public class ArtistAlbumFragment extends BaseFragment implements RankClassAdapter.OnClickToShareLinstener {
    private View v;
    private TextView updata, total;
    private ImageButton playAll, share;
    private UMSocialService mController;
    private Context context;
    private ListViewForScrollView listView;
    private ImageView imageView;
    private VolleySingleton requestQueue;
    private StringRequest request;
    private List<AlbumDetailEntity.SonglistEntity> albumDetailEntities;
    private List<SongListEntity> changeList;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_artist_album, null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Qzone分享
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qZoneSsoHandler.addToSocialSDK();

        // qq分享
        UMQQSsoHandler qqssoH = new UMQQSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qqssoH.addToSocialSDK();

        // 设置分享内容
        QQShareContent qqcontent = new QQShareContent();
        qqcontent.setShareContent("分享个音乐");
        mController.setShareMedia(qqcontent);

        Bundle bundle = getArguments();
        String id = bundle.getString("album_id");
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.album.getAlbumInfo&format=json&album_id=" + id;

        // 给页面设置工具栏
        final Toolbar toolbar = (Toolbar) v.findViewById(R.id.artist_album_toolbar);
        toolbar.setNavigationIcon(R.mipmap.bt_return_nor);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });

        // 解析
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                AlbumDetailEntity albumDetailEntity = gson.fromJson(response, AlbumDetailEntity.class);
                albumDetailEntities = albumDetailEntity.getSonglist();
                total.setText(albumDetailEntity.getSonglist().size() + "");
                updata.setText(albumDetailEntity.getAlbumInfo().getPublishtime());
                Picasso.with(context).load(albumDetailEntity.getAlbumInfo().getPic_big()).into(imageView);
                // 设置toolbar文字
                toolbar.setTitle(albumDetailEntity.getAlbumInfo().getTitle());

                // 遍历实体类集合,转换实体类,以复用adapter
                for (AlbumDetailEntity.SonglistEntity entity : albumDetailEntities) {
                    SongListEntity songListEntity = new SongListEntity();
                    songListEntity.setAlbum_id(entity.getAlbum_id());
                    songListEntity.setAll_artist_id(entity.getAll_artist_id());
                    songListEntity.setTitle(entity.getTitle());
                    songListEntity.setTing_uid(entity.getTing_uid());
                    songListEntity.setLanguage(entity.getLanguage());
                    songListEntity.setAuthor(entity.getAuthor());
                    songListEntity.setLrclink(entity.getLrclink());
                    songListEntity.setSong_id(entity.getSong_id());
                    songListEntity.setArtist_name(entity.getAuthor());
                    songListEntity.setArtist_id(entity.getArtist_id());
                    songListEntity.setPic_big(entity.getPic_big());
                    changeList.add(songListEntity);
                }
                RankClassAdapter adapter = new RankClassAdapter(context, changeList);
                adapter.setOnClickToShareLinstener(ArtistAlbumFragment.this);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.addQueue(request);
        // 点击事件遍历,传值,播放
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (AlbumDetailEntity.SonglistEntity entity : albumDetailEntities) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });

        // 播放所有,从下标=0开播
        playAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (AlbumDetailEntity.SonglistEntity entity : albumDetailEntities) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, 0);
            }
        });


    }

    private void init() {
        imageView = (ImageView) v.findViewById(R.id.artist_album_backdrop);
        playAll = (ImageButton) v.findViewById(R.id.artist_album_playAll);
        updata = (TextView) v.findViewById(R.id.artist_album_updata_time);
        listView = (ListViewForScrollView) v.findViewById(R.id.artist_album_listView);
        share = (ImageButton) v.findViewById(R.id.artist_album_all_share_imageButton);
        context = getActivity();
        mController = UMServiceFactory.getUMSocialService(getString(R.string.UMSocialService));
        requestQueue = VolleySingleton.getInstance(context);
        changeList = new ArrayList<>();
        total = (TextView) v.findViewById(R.id.artist_album_total);
    }

    // 点击分享
    @Override
    public void onClickToShare(SongListEntity songListEntity, int position) {
        mController.openShare(getActivity(), false);
    }
}
